import request from '@/utils/request'
import { GFCD } from '@/utils/utils'
import isEmpty from 'lodash/isEmpty'
import { cloneDeep } from 'lodash'
import { translateTitle } from '@/utils/i18n'
export default {
  computed: {
    getSelectModule() {
      return this.$store.getters['diy/getSelectModule']
    },
  },
  methods: {
    $formateArrToObj(arr) {
      let obj = {}
      arr.map((item) => {
        obj[item.key] = item.value
      })
      return obj
    },
    $getResponseData(arr, formatte) {
      return arr.map((item) => {
        return {
          name: item[formatte.name],
          value: item[formatte.value] || 0,
          chartName: formatte.titleName,
        }
      })
    },
    format(dat) {
      //获取年月日，时间
      var year = dat.getFullYear()
      var mon = dat.getMonth() + 1 < 10 ? '0' + (dat.getMonth() + 1) : dat.getMonth() + 1
      var data = dat.getDate() < 10 ? '0' + dat.getDate() : dat.getDate()
      var hour = dat.getHours() < 10 ? '0' + dat.getHours() : dat.getHours()
      var min = dat.getMinutes() < 10 ? '0' + dat.getMinutes() : dat.getMinutes()
      var seon = dat.getSeconds() < 10 ? '0' + dat.getSeconds() : dat.getSeconds()

      var newDate = year + '-' + mon + '-' + data + ' ' + hour + ':' + min + ':' + seon
      return newDate
    },
    onInputValue(arr) {
      return arr.map((item) => {
        let obj = cloneDeep(item)
        if (item.type == 'date') {
          if (item.num == 0) {
            obj.value = this.format(new Date())
          } else if (item.num > 0) {
            obj.value = this.format(new Date(new Date(new Date().toLocaleDateString()) - 86400000 * (obj.num - 1) - 1000))
          }
        }
        console.log(obj)
        return obj
      })
    },
    $request(options, cb) {
      // let options = cloneDeep(this.infoObj)
      if (isEmpty(options.api) || isEmpty(options.method)) {
        this.$message.error(translateTitle('Ve.mixin.api提示'))
        return
      }
      // if (!options.resList.length) {
      //   this.$message.error('请填写响应值')
      //   return
      // }
      if (options.loopRequest && options.loopTime <= 0) {
        this.$message.error(translateTitle('Ve.mixin.循环值提示'))
        return
      }
      this.loading = true
      console.log(Array.isArray(options.data))
      options.data = Array.isArray(options.data) ? this.onInputValue(options.data) : options.data

      let _data = Array.isArray(options.data) ? this.$formateArrToObj(options.data) : options.data
      return request({
        url: GFCD('dams', 'domain') + options.api,
        method: 'post',
        data: {
          data: JSON.stringify(_data),
          method: options.method,
        },
      }).then((res) => {
        cb(res['data']?.[options.getDataKey] ? res['data']?.[options.getDataKey]?.data : res['data'], res['data']?.[options.getDataKey]?.count)
      })
    },
    formatterOption(obj) {
      let rObj = {}
      for (const key in obj) {
        if (key.indexOf('_') > -1) {
          rObj[key.split('_')[0]] = Object.assign(this.stringToobj(key, {}, 1), rObj[key.split('_')[0]])
        } else {
          rObj[key] = obj[key].value != undefined ? obj[key].value : obj[key]
        }
      }
      return rObj
    },
    stringToobj(str, obj, index) {
      let arr = str.split('_')
      if (index < arr.length - 1) {
        obj[arr[index]] = this.stringToobj(str, {}, index + 1)
      } else {
        obj[arr[index]] = this.info.extension[str].value != undefined ? this.info.extension[str].value : this.info.extension[str]
      }
      return obj
    },
  },
}
